package leetcode2;

import java.util.Random;

public class Day17Sort {

    public static void main(String[] args) {
        int[] arr = {10,9,8,7,6,5,4,3,2,1};
        quickSort(arr,0, arr.length-1);
        System.out.println();
    }

    /**
     * 快速排序
     *
     * @param array
     */
    public static void quickSort(int[] array,int start,int end) {
        if(start >= end) return;
        //1.随机选择基准元素
        int rand = new Random().nextInt(end - start + 1) + start;
        int patition = array[rand];
        int left = start-1,right=end+1,i=start;
        while (i<right) {
            if(array[i]>patition) swap(array,i,--right);
            else if (array[i] < patition) swap(array,i++,++left);
            else i++;
        }
        //继续递归:[start,left] [right,end]
        quickSort(array,start,left);
        quickSort(array,right,end);
    }

    private static void swap(int[] arr,int i,int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }



    public static void mergerSort(int[] array,int left,int right) {
        //1.递归
        //2.合并
        if(left>=right) return;
        int mid = (right+left)>>1; //划分两边
        //[left,mid] ,(mid,right]
        mergerSort(array,left,mid);
        mergerSort(array,mid+1,right);

        //开始合并，最开始只有两个数进行合并

    }



    /**
     *  堆排序
     *
     * @param
     */
    public static void deapSort(int[] array) {

    }



}
